package nl.ucan.navigate;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.util.List;
import java.util.ArrayList;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
/*
 * Copyright 2007-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * author : Arnold Reuser
 * since  : 0.1
  */

public class GenericTypeUtil {
    public static Class<?>[] getActualTypeArguments(String property,Class clasz) throws IntrospectionException {
		List<Class<?>> types = new ArrayList<Class<?>>();
		PropertyDescriptor propertyDescriptor = new PropertyDescriptor(property, clasz);
		Method method = propertyDescriptor.getReadMethod();
		ParameterizedType genericReturnType = (ParameterizedType)method.getGenericReturnType();
		Type[] actualTypeArguments = genericReturnType.getActualTypeArguments();
        for ( int i=0 ; i < actualTypeArguments.length ; i ++) {
        	Class<?> type = (Class<?>)(actualTypeArguments[i]); // could be class or interface
        	types.add(type);
        }
        return (Class<?>[])types.toArray(new Class<?>[types.size()]);
	}
}
